// i.r(t), i.d(t, { default: () => SweepPortalNavigation })
import { NavigateToSweepCommand } from "../command/navigate.command"
import keyConst from "../constant/key.const"
import CameraData from "../data/CameraData"
import SettingsData from "../data/SettingsData"
import SweepData from "../data/SweepData"
import ViewmodeData from "../data/ViewmodeData"
import meshStateEnum from "../enum/meshState.enum"
import sweepAlignmentEnum from "../enum/sweepAlignment.enum"
import sweepPlacementEnum from "../enum/sweepPlacement.enum"
import transitionTypeEnum from "../enum/transitionType.enum"
import viewmodeEnum from "../enum/viewmode.enum"
import geoFT from "../math/geoFT"
import { PortalClickMessage } from "../message/portal.message"
import funcUtil from "../utils/func.util"
import { isPanOrMesh } from "../utils/viewmode.util"
import Module from "./Module"
import PortalPuckMeshModule from "./PortalPuckMeshModule"
import ViewmodeModule from "./ViewmodeModule"

const r = funcUtil.getUrlParams("geodist", geoFT.getM(funcUtil.getUrlParams("geodistft", 20)))
export default class SweepPortalNavigation extends Module {
  onChange: any
  portalRenderer: any
  settingsData: any
  viewmodeData: any
  sweepData: any
  cameraData: any
  viewmodeModule: any
  constructor() {
    super(...arguments),
      (this.name = "sweep-portal-navigation"),
      (this.onChange = () => {
        this.portalRenderer.filter(e => {
          if (!this.settingsData.tryGetProperty(keyConst.rule.features.views, !0)) return meshStateEnum.HIDE
          if (!isPanOrMesh(this.viewmodeData.closestMode)) return meshStateEnum.HIDE
          const t = this.sweepData.getSweep(this.sweepData.currentSweep || "")
          if (t)
            if (t.alignmentType === sweepAlignmentEnum.ALIGNED) {
              if (e.fromInterior && e.toExterior && e.position.distanceTo(t.position) < r) return meshStateEnum.SHOW
            } else if (t.placementType === sweepPlacementEnum.MANUAL && e.fromSweep.id === t.id) return meshStateEnum.ONTOP
          return meshStateEnum.HIDE
        })
      })
  }
  async init(e, t) {
    ;[this.portalRenderer, this.cameraData, this.viewmodeModule, this.viewmodeData, this.sweepData, this.settingsData] = await Promise.all([
      t.getModule(PortalPuckMeshModule),
      t.market.waitForData(CameraData),
      t.getModule(ViewmodeModule),
      t.market.waitForData(ViewmodeData),
      t.market.waitForData(SweepData),
      t.market.waitForData(SettingsData)
    ])
    const i = (e, i?) => {
      this.viewmodeData.currentMode &&
        this.cameraData.canTransition() &&
        (this.viewmodeData.currentMode !== viewmodeEnum.Panorama
          ? this.viewmodeModule.switchToMode(viewmodeEnum.Panorama, transitionTypeEnum.FadeToBlack, {
              sweepID: e,
              rotation: i
            })
          : t.commandBinder.issueCommand(
              new NavigateToSweepCommand({
                sweep: e,
                rotation: i,
                transition: transitionTypeEnum.FadeToBlack
              })
            ))
    }
    this.settingsData.onPropertyChanged(keyConst.rule.features.views, this.onChange),
      this.bindings.push(
        t.subscribe(PortalClickMessage, e => {
          i(e.toSweep.id)
        }),
        this.viewmodeData.onChanged(this.onChange),
        this.sweepData.onChanged(this.onChange)
      ),
      this.onChange()
  }
}
